module test_aio;

private import dactor.scheduler;
private import dactor.coroutine;
private import dactor.eventlet;
private import dactor.log;
private import core.sys.posix.unistd;
private import core.sys.posix.fcntl;
private import core.sys.posix.signal;
private import std.stdio;
private import linux.aio;


private import core.stdc.time;

void test_aio() {
    int f = open(__FILE__, O_RDONLY | O_CREAT, 0600);
    if (f == -1) {
        logDebug("open fail");
        return;
    }

    scope(exit) close(f);

    while (true) {

        char[] buffer = new char[10240];
        scope auto e = new AioEvent!(35)(f, buffer, Event.READ, 0);

        time_t start = time(null);
        int r = e.wait();
        time_t end = time(null);
        writefln("wait result: %d, aio result: %d, time used: %f", r, e.aioRet, difftime(start, end));
        if (e.aioRet >= 0) {
            writefln("file context: %s", buffer[0..e.aioRet]);
        }
    }
}

void main()
{
    spawn(&test_aio);

    Scheduler.instance.run();
}

